from threading import Thread
import cv2
import pytesseract
import pyautogui
import time
import datetime
import time
from fileinput import filename

from PIL import ImageGrab

def goleft():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('1')
    pyautogui.press('7')
    # 点击右坐标     
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    pyautogui.moveTo(1481, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')

def fight2():
    print("开始刷怪")
    for i in range(15):
        print("第"+str(i)+"轮")
        rightMoveAndLeftA()
        goleft()
        time.sleep(8.5)
        RightAandMove()

def fight():
    for i in range(20):
        rightMoveAndLeftA()
        for i in range(3):
            leftAndRight()


def gohome():
    #点击寻路
    pyautogui.moveTo(1740,330,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击下拉框
    pyautogui.moveTo(1515, 424, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #拖动下拉框
    pyautogui.moveTo(1513,526,0.1)
    pyautogui.mouseDown()
    time.sleep(6)
    pyautogui.mouseUp()
    #点击江陵
    pyautogui.moveTo(1262,540,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('2')
    pyautogui.press('0')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    #点击移动
    pyautogui.moveTo(1481, 747, 0.1)
    for i in range(20):
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        time.sleep(1)
    pyautogui.press('esc')
    pyautogui.press('D')
    time.sleep(20)

def goWork():
    print("开始前往蜀国蓄水场")
    #点击寻路
    pyautogui.moveTo(1740,330,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击下拉框
    pyautogui.moveTo(1515, 424, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #拖动下拉框
    pyautogui.moveTo(1509,597,0.1)
    pyautogui.mouseDown()
    time.sleep(5)
    pyautogui.mouseUp()
    #点击蓄水场
    pyautogui.moveTo(1276,640,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    #点击移动
    pyautogui.moveTo(1481, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(30)

    #到达蓄水场
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    pyautogui.moveTo(1481, 747, 0.1)
    #
    for i in range(10):
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        time.sleep(2)
    pyautogui.press('esc')
    print("到达蜀国蓄水场")

def shopping():
    pyautogui.press('G')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('enter')
    time.sleep(1)
    getImg()
    if redImg():
        #移动到商品1
        time.sleep(0.5)
        pyautogui.moveTo(1058, 951, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(906, 961, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(1121, 962, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        #移动到商品2
        time.sleep(0.5)
        pyautogui.moveTo(1058, 1001, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(906, 1011, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(1121, 1012, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        #点击交易
        pyautogui.moveTo(1564, 1047, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
    pyautogui.press('esc')
def up():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()

    #点击左坐标
    pyautogui.moveTo(1535, 743, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('9')
    #点击右坐标
    pyautogui.moveTo(1574, 741, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    #点击移动
    pyautogui.moveTo(1741, 731, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')
    time.sleep(7)

def down():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()

    #点击左坐标
    pyautogui.moveTo(1535, 743, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1574, 741, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    #点击移动
    pyautogui.moveTo(1741, 731, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')
    time.sleep(5)

def rightMoveAndLeftA():
    pyautogui.keyDown('right')
    time.sleep(0.5)
    pyautogui.keyUp('right')
    pyautogui.keyDown('left')
    pyautogui.keyUp('left')
    pyautogui.press("A")
    time.sleep(1.9)

def RightAandMove():
    pyautogui.keyDown('right')
    pyautogui.keyUp('right')
    pyautogui.press("A")
    time.sleep(2)
    pyautogui.keyDown('right')
    time.sleep(8)
    pyautogui.keyUp('right')


def leftAndRight():
    pyautogui.keyDown('left')
    time.sleep(3.1)
    pyautogui.keyUp('left')
    pyautogui.keyDown('right')
    time.sleep(3.1)
    pyautogui.keyUp('right')

def getImg():
    filename='NUM.PNG'
    print(filename)
    ImageGrab.grab(bbox=(890, 940, 920, 972)).save(filename)

def redImg():
    # 读取图片
    image = cv2.imread("NUM.PNG")
    # 将图片转换为灰度图
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 使用Tesseract - OCR识别数字
    text = pytesseract.image_to_string(gray_image, config='--psm 10 -c tessedit_char_whitelist=0123456789')
    print("当前数量为"+text)
    if text == "":
        print("只剩1个不卖")
        return False
    else:
        print("可以卖")
        return True

# 按下Q键
time.sleep(1)


while(True):
    goWork()
    fight2()
    gohome()
    shopping()

